package com.example.dolgi;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class Item1 extends Activity {
	//Never forget to leave comments!!
	private static final int CM_DELETE_ID = 1;
	final String ATTRIBUTE_NAME_TEXT = "text";
	final String ATTRIBUTE_NAME_IMAGE = "image";
	ListView lvSimple;
	//SimpleAdapter sAdapter;
	//ArrayList<Map<String, Object>> data;
	EditText etName,etDebt;
	
	ProgressDialog pd;
	List<User> data;
	ArrayAdapter<User> ad;
	
	private UserDebtDAO _dbAsset;
	//using async task to prevent application unresponsiveness till DB is loaded
	//rtm for it
	class DBInfoLoader extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... arg0) {
			data = _dbAsset.getAllUsers();
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			ad = new ArrayAdapter<User>(getApplicationContext(), android.R.layout.simple_list_item_1, data);
			lvSimple.setAdapter(ad);
			pd.dismiss();
		}
	}
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item1);
		//sorry for blocking this, no time to go deeper without comments
		
		/*data = new ArrayList<Map<String, Object>>();
		String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE };
		int[] to = { R.id.tvText, R.id.ivImg };
		sAdapter = new SimpleAdapter(this, data, R.layout.item1add, from, to);
		lvSimple.setAdapter(sAdapter);
		
		*/
		lvSimple = (ListView) findViewById(R.id.lvSimple);
		etName = (EditText) findViewById(R.id.etName);
		etDebt = (EditText) findViewById(R.id.etDebt);
		registerForContextMenu(lvSimple);
		
		//loading database
		_dbAsset = new UserDebtDAO(this);
		//opening database to read data
		_dbAsset.open();
		//loading all data
		pd = ProgressDialog.show(this, "Loading...", "Loading, please, wait...");
		pd.setCancelable(false);
		new DBInfoLoader().execute();
	}
	public void onButtonClick(View v) {
		//m = new HashMap<String, Object>();
		//m.put(ATTRIBUTE_NAME_TEXT, );
		//m.put(ATTRIBUTE_NAME_IMAGE, etDebt.getText().toString());
		//data.add(m);
		
		//creating new user in DB and linkin' it to list...
		User newUser = _dbAsset.createUser(etName.getText().toString(),etDebt.getText().toString());
		
		//adding him to data resourse
		data.add(newUser);
		//ad.add(newUser);
		ad.notifyDataSetChanged();
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		Resources r = getResources();
		String value = r.getString(R.string.contextMenu);
		menu.add(0, CM_DELETE_ID, 0, value);
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if (item.getItemId() == CM_DELETE_ID) {
			AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) item.getMenuInfo();
			User removing = data.get(acmi.position);
			data.remove(acmi.position);
			ad.notifyDataSetChanged();
			_dbAsset.deleteUser(removing);
			System.out.println("User info:"+removing);
			return true;
		}
		return super.onContextItemSelected(item);
	}
}
